﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace SGArchiveSystem7x.Controllers.Borrow
{
    public class InsidersController : BaseController
    {
        // GET: 内部借阅
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 借阅档案（此方法不用权限判定，都可以申请）
        /// </summary>
        /// <param name="jsonBrr">传的JSON数据</param>
        /// <param name="type">电子借阅还是纸质借阅， 电子为1， 纸质为2</param>
        /// <returns></returns>
        public ActionResult BrrArchive(string type)//string jsonBrr
        {
            var user = App_Code.CSession.Get_User(Session);
            if (user == null)
            {
                ////如果SESSION丢失，跳转到Sesion丢失页面
                return View();
            }
            ViewBag.User = user;

            var listColumns = Models.CColumnsModel.Create_BrrColumns();
            ViewBag.Columns = listColumns;

            var listLYMD = Models.CComboboxModel.Create_ComboboxLYMD(false);
            ViewBag.LYMD = listLYMD;

            //JavaScriptSerializer js = new JavaScriptSerializer();
            //var list = js.Deserialize<List<Models.CBrrModel>>(jsonBrr);
            //App_Code.CSession.Add_Brr(Session, list);
            ViewBag.type = type;
            return View();
        }
        /// <summary>
        /// 添加借阅的session
        /// </summary>
        /// <param name="jsonBrr"></param>
        /// <returns></returns>
        public string Add_BrrArchive(string jsonBrr)
        {
            var user = App_Code.CSession.Get_User(Session);
            JavaScriptSerializer js = new JavaScriptSerializer();
            var list = js.Deserialize<List<Models.CBrrModel>>(jsonBrr);//档案类型的中文名没加
            string result = "1";
            foreach(var nn in list)
            {
                if (SGArchiveSystem7xDB.Linq.T_BInfoDal.Get_BrrInfo(user.U_ID, nn.DH))
                {
                    result = "0";
                    break;
                }
            }
            if (result == "1")
            {
                foreach (var en in list)
                {
                    var listArchive = SGArchiveSystem7xDB.Linq.T_ArchiveDal.Get_SingleArchive(en.DH);
                    en.ARCHIVEID = listArchive.ID;
                    var listTree = SGArchiveSystem7xDB.Linq.T_ArchiveTreeDal.Get_SingleTree(en.ARID);
                    en.ATYPE = listTree.AR_NAME;
                    en.TAG = listArchive.DZWJ;
                    en.AJORJN = 1;
                    var listword = SGArchiveSystem7xDB.Linq.T_ArchiveWordDal.Get_ArchiveWord(listArchive.MJ);
                    en.BRRMJ = listword.WD_NAME;

                    if(en.ATYPE == "诉讼档案")
                    {
                        en.TM = "案由:" + listArchive.GJZ + " 被告:" + en.TM; 
                    }
                }
                App_Code.CSession.Add_Brr(Session, list);
            }
            return result;
        }
        public string Del_BrrSession(string dh)
        {
            var list = App_Code.CSession.Get_Brr(Session);
            foreach (var en in list)
            {
                if (en.DH == dh)
                {
                    list.Remove(en);
                    break;
                }
            }
            App_Code.CSession.Add_Brr(Session, list);
            return "1";
        }
        /// <summary>
        /// Ajax获取借阅数据
        /// </summary>
        /// <returns></returns>
        public ActionResult Get_ajaxBrrArchive()
        {
            var list = App_Code.CSession.Get_Brr(Session);
            var json = new
            {
                rows = list.ToArray(),
                total = list.Count
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 借阅档案
        /// </summary>
        /// <returns></returns>
        public string Add_Brr(string entities, string type, string lymdid, string lymd, string YJGUTime, string bname, string cksj)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            var list = js.Deserialize<List<Models.CBrrModel>>(entities);
            var user = App_Code.CSession.Get_User(Session);
            var dep = SGArchiveSystem7xDB.Linq.T_DeparetmentDal.Get_Department(int.Parse(user.U_DEPID.ToString()));
            SGArchiveSystem7xDB.T_BUser bUser = new SGArchiveSystem7xDB.T_BUser();
            bUser.BU_TYPE = int.Parse(type);
            bUser.BU_BUSERID = user.U_ID;
            ////登记的借阅人
            bUser.BU_BUSERNAME = bname;     ////user.U_RNAME;
            bUser.BU_PHONE = user.U_V04;//v01 phone
            bUser.BU_EMAIL = user.U_V01;//v02 email
            bUser.BU_STATUS = (int)SGArchiveSystem7xDB._BrrStatus._iStatus_申请;
            bUser.BU_DATE = DateTime.Now;
            bUser.BU_LYMD = lymd;
            bUser.BU_LYMDID = int.Parse(lymdid);
            bUser.BU_V01 = dep.Dep_Name;
            ///登记人
            bUser.BU_V02 = user.U_RNAME;
            

            int value = SGArchiveSystem7xDB.Linq.T_BuserDal.Add_Buser(bUser);
            int result = 0;
            SGArchiveSystem7xDB.T_BInfo en = new SGArchiveSystem7xDB.T_BInfo();
            for (int i = 0; i < list.Count(); i++)
            {
                en = new SGArchiveSystem7xDB.T_BInfo();
                en.BI_BUID = value;
                en.BI_ATYPE = list[i].ATYPE;
                en.BI_ARID = list[i].ARID;
                en.BI_AJORJN = list[i].AJORJN;
                en.BI_ARCHIVEID = list[i].ARCHIVEID;
                en.BI_JNID = list[i].JNID;
                en.BI_DH = list[i].DH;
                en.BI_TM = list[i].TM; 
                en.BI_V01 = list[i].BRRMJ;
                en.BI_YJGHDATE = Convert.ToDateTime(YJGUTime);
                en.BI_V02 = list[i].TAG;

                ////出库时间
                if (cksj == "1")
                    en.BI_LQDATE = bUser.BU_DATE.AddHours(1);
                else if (cksj == "2")
                    en.BI_LQDATE = bUser.BU_DATE.AddHours(2);
                else if (cksj == "3")
                    en.BI_LQDATE = new DateTime(bUser.BU_DATE.Year, bUser.BU_DATE.Month, bUser.BU_DATE.Day, 23, 59, 59);
                else if (cksj == "4")
                    en.BI_LQDATE = new DateTime(2020, 12, 31);

                en.BI_STATUS = (int)SGArchiveSystem7xDB._BrrStatus._iStatus_申请; // (int)SGArchiveSystem7xDB._BrrStatus._iStatus_申请;
                //SGArchiveSystem7xDB.SGArchiveSystem7xEntities conn = new SGArchiveSystem7xDB.SGArchiveSystem7xEntities();
                //var flow = conn.T_SP.Select(c => c).Where(c => c.SP_ID == 1).Single();//目前默认取第一个，以后完善时根据密级来判断 
                //string[] arr = flow.SP_STATUS.Split(',');
                //for(int j=0;j<arr.Length;j++)
                //{
                //    if (arr[j] == "1")
                //    {
                //        if (j == 0)
                //        {
                //            en.BI_SPSTATUS = 2;
                //            break;
                //        }
                //        else if(j==1)
                //        {
                //            en.BI_SPSTATUS = 3;
                //            break;
                //        }
                //        else 
                //        {
                //            en.BI_SPSTATUS = 0;
                //            break;
                //        }
                //    }                   
                //}
                ///设为审批通过
                en.BI_SPVALUE = (int)SGArchiveSystem7xDB._BrrSPValue._iValue_审批通过; //(int)SGArchiveSystem7xDB._BrrSPValue._iValue_未审批;
                en.BI_YQTS = 0;
                result = SGArchiveSystem7xDB.Linq.T_BInfoDal.Add_Brr(en, int.Parse(type));

                #region 整个审批流程取消
                //if (en.BI_SPSTATUS == 0)//直接进入审批流程
                //{
                //    int count = 0;
                //    var listToMail = SGArchiveSystem7xDB.Linq.T_EmailDal.Get_Email(1, out count);
                //    string[] email = new string[listToMail.Count()];
                //    for (int n = 0; n < listToMail.Count(); n++)
                //    {
                //        email[n] = listToMail[n].E_EMAIL;
                //    }
                //    SGArchiveSystem7xDB.Linq.T_BInfoDal.PrepareEmail("申请借阅", user.U_RNAME + "对档号为" + list[i].DH + "，题名为" + list[i].TM + "的档案发出了申请", email);
                //}
                //else if (en.BI_SPSTATUS == 2)//下一步本部门审批
                //{
                //    SGArchiveSystem7xDB.T_SPFlow en1 = new SGArchiveSystem7xDB.T_SPFlow();
                //    en1.SPF_UserDep = user.U_DEPID;
                //    en1.SPF_AJDep = list[i].GDBM;
                //    en1.SPF_BID = result;
                //    en1.SPF_Stage = 1;//需要去T_SP表中查找需要审批的步骤，目前默认为1
                //    en1.SPF_Status = 0;//还没审批
                //    SGArchiveSystem7xDB.Linq.T_SPFlowDal.Add_SPFlow(en1);

                //    var listEmail = SGArchiveSystem7xDB.Linq.T_UserDal.GET_EMAIL(user.U_DEPID, 1);
                //    string[] email = new string[listEmail.Count()];
                //    for (int n = 0; n < listEmail.Count(); n++)
                //    {
                //        email[n] = listEmail[n].U_V01;
                //    }
                //    SGArchiveSystem7xDB.Linq.T_BInfoDal.PrepareEmail("申请借阅", user.U_RNAME + "对档号为" + list[i].DH + "，题名为" + list[i].TM + "的档案发出了申请", email);
                //}
                //else//下一步档案归属审批
                //{
                //    SGArchiveSystem7xDB.T_SPFlow en1 = new SGArchiveSystem7xDB.T_SPFlow();
                //    en1.SPF_UserDep = user.U_DEPID;
                //    en1.SPF_AJDep = list[i].GDBM;
                //    en1.SPF_BID = result;
                //    en1.SPF_Stage = 2;//需要去T_SP表中查找需要审批的步骤，目前默认为1
                //    en1.SPF_Status = 0;//还没审批
                //    SGArchiveSystem7xDB.Linq.T_SPFlowDal.Add_SPFlow(en1);

                //    var listEmail = SGArchiveSystem7xDB.Linq.T_UserDal.GET_EMAIL(list[i].GDBM, 1);
                //    string[] email = new string[listEmail.Count()];
                //    for (int n = 0; n < listEmail.Count(); n++)
                //    {
                //        email[n] = listEmail[n].U_V01;
                //    }
                //    SGArchiveSystem7xDB.Linq.T_BInfoDal.PrepareEmail("申请借阅", user.U_RNAME + "对档号为" + list[i].DH + "，题名为" + list[i].TM + "的档案发出了申请", email);
                //}
                #endregion
            }
            if (result > 0)
                return "1";
            else
                return "0";
        }
        /// <summary>
        /// 添加水印，返回水印pdf路径
        /// </summary>
        /// <param name="inputfilepath">原pdf名</param>
        /// <param name="word">水印文字</param>
        /// /// <param name="OUT">随机的文件名</param>
        /// <returns></returns>
        public string AddWaterMark(string inputfilepath, string OUT, string word)
        {
            string outputfilepath = "/Content/OutPDF/" + OUT + "/" + inputfilepath + "_sy.pdf";
            if (System.IO.File.Exists(Server.MapPath(outputfilepath)))
            {
                System.IO.File.Delete(Server.MapPath(outputfilepath));
            }
            SGArchiveSystem7xCommons.WaterMark.PDFWatermark(Server.MapPath("/Content/OutPDF/" + OUT + "/" + inputfilepath), Server.MapPath(outputfilepath), word);

            return outputfilepath;
        }
        /// <summary>
        /// 处理pdf压缩包
        /// </summary>
        /// <param name="path">压缩包路径</param>
        /// <param name="iswaterwark">是否加水印</param>
        /// <returns></returns>
        public string pdfpath(string id,string path,string dh, string iswaterwark = "FALSE")
        {
            //文件夹名
            string filename = DateTime.Now.Ticks.ToString();
            string PDFpath = "";
            var user = App_Code.CSession.Get_User(Session);
            string value = "";
            var listBinfo = SGArchiveSystem7xDB.Linq.T_ArchiveDal.Get_SingleArchive(dh);
            if (id != null && id != "")
                listBinfo = SGArchiveSystem7xDB.Linq.T_ArchiveDal.Get_SingleArchive(int.Parse(id));
            if(dh!=null&&dh!="")
                listBinfo = SGArchiveSystem7xDB.Linq.T_ArchiveDal.Get_SingleArchive(dh);
            if (path != null && path != "")
                PDFpath = path;
            else
                PDFpath = listBinfo.DZWJ;
            if (PDFpath != null && PDFpath != "")
            {
                var aridname = SGArchiveSystem7xDB.Linq.T_ArchiveTypeDal.Get_ArchiveType(listBinfo.ARID);
                SGArchiveSystem7xDB.T_LOGVIEW en = new SGArchiveSystem7xDB.T_LOGVIEW();
                en.LogView_USERNAME = user.U_RNAME;
                en.LogView_USERID = user.U_ID;
                en.LogView_TM = listBinfo.TM;
                en.LogView_DH = listBinfo.DH;
                en.LogView_AID = listBinfo.ARID;
                en.LogView_DateTime = DateTime.Now;
                en.LogView_Print = 0;
                en.LogView_AIDNAME = aridname.ATYPE_NAME;
                if (SGArchiveSystem7xCommons.SGZip.DecomparessFile(Server.MapPath("~/Content/PDF/" + PDFpath), Server.MapPath("~/Content/OutPDF/" + filename), null))
                {
                    if (iswaterwark == "TRUE")
                    {
                        value = AddWaterMark(PDFpath.Substring(0, PDFpath.Length - 3), filename, user.U_RNAME);
                    }
                    else
                    {
                        value = "/Content/OutPDF/" + filename + "/" + PDFpath.Substring(0, PDFpath.Length - 3);
                    }
                }
                else
                {
                    value = "1";
                }
                if (value != "1")
                {
                    SGArchiveSystem7xDB.Linq.T_LOGVIEWDal.Add_LogView(en);
                }
            }
            else
            {
                value = "1";
            }
            return value;
        }
        /// <summary>
        /// 审批界面
        /// </summary>
        /// <param name="spvalue">审批结果，0未审批1通过2不通过</param>
        /// <param name="type">1电子借阅2纸质借阅</param>
        /// <returns></returns>
        public ActionResult SPIndex(string type)
        {
            var columns = SGArchiveSystem7x.Models.CColumnsModel.Create_SPColumns(false,false,false,true);
            ViewBag.Columns = columns;
            ViewBag.BrrStatus = "0";
            ViewBag.Type = type;
            if (type == "1")
                ViewBag.NAME = "电子审批";
            else
                ViewBag.NAME = "纸质审批";
            return View();
        }
        /// <summary>
        /// 获取审批信息
        /// </summary>
        /// <param name="spvalue"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Get_ajaxSPArchive(string brrstatus, string type)
        {
            int count;
            var list = SGArchiveSystem7xDB.Linq.T_BInfoDal.Get_SPInfo(int.Parse(brrstatus),4, int.Parse(type),null,null,null,null,null,-1,out count, 0, SGArchiveSystem7x.App_Code.CWebCommons._iPageRows);
            List<string> list1 = new List<string>(4);
            list1.Add(brrstatus);
            list1.Add(type);
            list1.Add("");
            list1.Add("");
            App_Code.CSession.Add_QueryWhere(Session, list1);
            var json = new
            {
                rows = list.ToArray(),
                total = count
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public ActionResult Get_ajaxSPArchivePager(int num, int size)
        {
            ///分页后的起始行
            int iStart = (num - 1) * size;
            var listWhere = SGArchiveSystem7x.App_Code.CSession.Get_QueryWhere(Session);
            int count = 0;
            var list = SGArchiveSystem7xDB.Linq.T_BInfoDal.Get_SPInfo(int.Parse(listWhere[0]), 4, int.Parse(listWhere[1]), listWhere[2], listWhere[3], null, null, null, -1, out count, iStart, SGArchiveSystem7x.App_Code.CWebCommons._iPageRows);
            var json = new
            {
                rows = list.ToArray(),
                total = count
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 审批
        /// </summary>
        /// <param name="result"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public string SP(string result, string[] id,string sg)
        {
            SGArchiveSystem7xDB.T_BInfo en = new SGArchiveSystem7xDB.T_BInfo();
            for (int i = 0; i < id.Length; i++)
            {
                var user = App_Code.CSession.Get_User(Session);
                en.BI_ID = int.Parse(id[i]);
                en.BI_SPDATE = DateTime.Now;
                en.BI_SPID = user.U_ID;
                en.BI_SPNAME = user.U_RNAME;
                en.BI_SPVALUE = int.Parse(result);
                en.BI_SPSTATUS = (int)SGArchiveSystem7xDB._BrrSPStatus._iStatus_已审批;
                en.BI_V01 = sg;
                SGArchiveSystem7xDB.Linq.T_BInfoDal.SP_Archive(en);
            }
            return "1";
        }
        /// <summary>
        /// 出入界面
        /// </summary>
        /// <param name="brrstatus"></param>
        /// <returns></returns>
        public ActionResult OutInKF(string brrstatus)
        {
            bool b = (brrstatus == "0" ? true : false);
            var columns = SGArchiveSystem7x.Models.CColumnsModel.Create_OutInKColumns(b);
            ViewBag.Columns = columns;
            ViewBag.BrrStatus = brrstatus;
            if (brrstatus=="0")
                ViewBag.NAME = "出库列表";
            else
                ViewBag.NAME = "入库列表";
            return View();
        }
        /// <summary>
        /// 出入库
        /// </summary>
        /// <param name="brrstatus"></param>
        /// <param name="name"></param>
        /// <param name="dh"></param>
        /// <param name="tm"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Get_ajaxOutInKFArchive(string brrstatus, string name, string dh, string tm)
        {
            int count;
            var list = SGArchiveSystem7xDB.Linq.T_BInfoDal.Get_OutInKFInfo(int.Parse(brrstatus), name, dh, tm, out count, 0, SGArchiveSystem7x.App_Code.CWebCommons._iPageRows);
            List<string> list1 = new List<string>(4);
            list1.Add(brrstatus);
            list1.Add(name);
            list1.Add(dh);
            list1.Add(tm);
            App_Code.CSession.Add_QueryWhere(Session, list1);
            var json = new
            {
                rows = list.ToArray(),
                total = count
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 出入库分页
        /// </summary>
        /// <param name="num"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Get_ajaxOutInKFArchivePager(int num, int size)
        {
            int iStart = (num - 1) * size;
            int count;
            var listWhere = SGArchiveSystem7x.App_Code.CSession.Get_QueryWhere(Session);
            var list = SGArchiveSystem7xDB.Linq.T_BInfoDal.Get_OutInKFInfo(int.Parse(listWhere[0]), listWhere[1], listWhere[2], listWhere[3], out count, iStart, SGArchiveSystem7x.App_Code.CWebCommons._iPageRows);
            var json = new
            {
                rows = list.ToArray(),
                total = count
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        public string ArchiveOutK(string[] id)
        {
            SGArchiveSystem7xDB.T_BInfo en = new SGArchiveSystem7xDB.T_BInfo();
            for (int i = 0; i < id.Length; i++)
            {
                var user = App_Code.CSession.Get_User(Session);
                en.BI_ID = int.Parse(id[i]);
                en.BI_CKID = user.U_ID;
                en.BI_CKNAME = user.U_RNAME;
                en.BI_D01 = DateTime.Now;
                SGArchiveSystem7xDB.Linq.T_BInfoDal.CK_Archive(en);
            }
            return "1";
        }
        public string ArchiveInK(string[] id)
        {
            SGArchiveSystem7xDB.T_BInfo en = new SGArchiveSystem7xDB.T_BInfo();
            for (int i = 0; i < id.Length; i++)
            {
                var user = App_Code.CSession.Get_User(Session);
                en.BI_ID = int.Parse(id[i]);
                en.BI_GHDJID = user.U_ID;
                en.BI_GHDJNAME = user.U_RNAME;
                en.BI_D02 = DateTime.Now;
                SGArchiveSystem7xDB.Linq.T_BInfoDal.GH_Archive(en);
            }
            return "1";
        }
        public ActionResult BrrNowView()
        {
            var columns = SGArchiveSystem7x.Models.CColumnsModel.Create_SPColumns(true,true,false,true);
            ViewBag.Columns = columns;
            ViewBag.BrrStatus = "1";
            return View();
        }
        /// <summary>
        /// 获取正在借阅浏览的信息
        /// </summary>
        /// <param name="brrstatus"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Get_ajaxBrrNowViewArchive(string brrstatus, string type,string name,string dh)
        {
            int count;
            int TYPE = -1;
            if (type != null && type != "")
                TYPE = int.Parse(type);
            var list = SGArchiveSystem7xDB.Linq.T_BInfoDal.Get_SPInfo(int.Parse(brrstatus), (int)SGArchiveSystem7xDB._BrrSPStatus._iStatus_已审批, TYPE,name,dh, null, null, null, -1, out count, 0, SGArchiveSystem7x.App_Code.CWebCommons._iPageRows);
            List<string> list1 = new List<string>(4);
            list1.Add(brrstatus);
            list1.Add(TYPE.ToString());
            list1.Add(name);
            list1.Add(dh);
            App_Code.CSession.Add_QueryWhere(Session, list1);
            var json = new
            {
                rows = list.ToArray(),
                total = count
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public ActionResult Get_ajaxBrrNowViewArchivePager(int num, int size)
        {
            ///分页后的起始行
            int iStart = (num - 1) * size;
            var listWhere = SGArchiveSystem7x.App_Code.CSession.Get_QueryWhere(Session);
            int count = 0;
            var list = SGArchiveSystem7xDB.Linq.T_BInfoDal.Get_SPInfo(int.Parse(listWhere[0]), (int)SGArchiveSystem7xDB._BrrSPStatus._iStatus_已审批, int.Parse(listWhere[1]), listWhere[2], listWhere[3], null, null, null, -1, out count, iStart, SGArchiveSystem7x.App_Code.CWebCommons._iPageRows);
            var json = new
            {
                rows = list.ToArray(),
                total = count
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        public ActionResult BrrHistory(string name)
        {
            if (name == null)
                ViewBag.NAME = "历史记录";
            else
                ViewBag.NAME = "借阅日志";
            var columns = SGArchiveSystem7x.Models.CColumnsModel.Create_SPColumns(true,false,true,true);
            ViewBag.Columns = columns;
            ViewBag.BrrStatus = "2";
            return View();
        }
        /// <summary>
        /// 获取借阅历史记录
        /// </summary>
        /// <param name="brrstatus"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Get_ajaxBrrHistoryArchive(string brrstatus, string type, string name, string dh,string stime, string etime)
        {
            int count;
            int TYPE = -1;
            if (type != null && type != "")
                TYPE = int.Parse(type);
            var list = SGArchiveSystem7xDB.Linq.T_BInfoDal.Get_SPInfo(-1, -1, TYPE,name,dh, stime, etime, null, -1, out count, 0, SGArchiveSystem7x.App_Code.CWebCommons._iPageRows);
            List<string> list1 = new List<string>(6);
            list1.Add("-1");
            list1.Add(TYPE.ToString());
            list1.Add(name);
            list1.Add(dh);
            list1.Add(stime);
            list1.Add(etime);
            App_Code.CSession.Add_QueryWhere(Session, list1);
            var json = new
            {
                rows = list.ToArray(),
                total = count
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public ActionResult Get_ajaxBrrHistoryArchivePager(int num, int size)
        {
            ///分页后的起始行
            int iStart = (num - 1) * size;
            var listWhere = SGArchiveSystem7x.App_Code.CSession.Get_QueryWhere(Session);
            int count = 0;
            var list = SGArchiveSystem7xDB.Linq.T_BInfoDal.Get_SPInfo(int.Parse(listWhere[0]), -1, int.Parse(listWhere[1]), listWhere[2], listWhere[3], listWhere[4], listWhere[5], null, -1, out count, iStart, SGArchiveSystem7x.App_Code.CWebCommons._iPageRows);
            var json = new
            {
                rows = list.ToArray(),
                total = count
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        public string YQGHEmai(string[] id)
        {
            for (int i = 0; i < id.Length; i++)
            {
                var query = SGArchiveSystem7xDB.Linq.T_BInfoDal.Get_Brr(int.Parse(id[i]));
                string[] email = new string[1] { query.BU_EMAIL };
                SGArchiveSystem7xDB.Linq.T_BInfoDal.PrepareEmail("逾期归还通知", "你借阅的档号为"+query.BI_DH+"，题名为"+query.BI_TM+"的纸质档案已经逾期"+query.BI_YQTS+"天了", email); ;
            }
            return "1";
        }
        public string EndBrr(string id)
        {            
            return SGArchiveSystem7xDB.Linq.T_BInfoDal.End_BRRArchive(int.Parse(id)).ToString();
        }
        public ActionResult PersonWorkSpace()
        {
            var columns = SGArchiveSystem7x.Models.CColumnsModel.Create_SPColumns(true, false, true, false);
            ViewBag.ColumnsQX = SGArchiveSystem7x.Models.CColumnsModel.Create_DepartmentArchiveListColumns(false);
            ViewBag.Columns = columns;
            ViewBag.ColumnsSP= SGArchiveSystem7x.Models.CColumnsModel.Create_SPFlowColumns();
            ViewBag.dept =  SGArchiveSystem7xDB.Linq.T_ArchiveTreeDal.Get_ArchiveTree(-1, 1);
            var user = App_Code.CSession.Get_User(Session);
            ViewBag.DepId = user.U_DEPID;
            ViewBag.U_I01= user.U_I01;
            return View();
        }
        /// <summary>
        /// 获取我的
        /// </summary>
        /// <param name="brrstatus"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Get_ajaxPersonWorkSpaceArchive(string tm,string type,string Atype,string stime,string etime)
        {
            int count;
            var user = App_Code.CSession.Get_User(Session);
            var list = SGArchiveSystem7xDB.Linq.T_BInfoDal.Get_SPInfo(-1,-1,int.Parse(type),null,null, stime, etime, tm, int.Parse(Atype),out count, 0, SGArchiveSystem7x.App_Code.CWebCommons._iPageRows, user.U_ID);
            //var list = SGArchiveSystem7xDB.Linq.T_BInfoDal.Get_SPInfo(-1, -1, -1, null, null, null, null, null, -1, out count, 0, SGArchiveSystem7x.App_Code.CWebCommons._iPageRows, user.U_ID);

            List<string> list1 = new List<string>(5);
            list1.Add(tm);
            list1.Add(type);
            list1.Add(Atype);
            list1.Add(stime);
            list1.Add(etime);
            App_Code.CSession.Add_QueryWhere(Session, list1);
            var json = new
            {
                rows = list.ToArray(),
                total = count
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public ActionResult Get_ajaxPersonWorkSpaceArchivePager(int num, int size)
        {
            ///分页后的起始行
            int iStart = (num - 1) * size;
            var user = App_Code.CSession.Get_User(Session);
            var listWhere = SGArchiveSystem7x.App_Code.CSession.Get_QueryWhere(Session);
            int count = 0;
            var list = SGArchiveSystem7xDB.Linq.T_BInfoDal.Get_SPInfo(-1, -1, int.Parse(listWhere[1]), null, null, listWhere[3], listWhere[4], listWhere[0], int.Parse(listWhere[2]), out count, iStart, SGArchiveSystem7x.App_Code.CWebCommons._iPageRows, user.U_ID);
            //var list = SGArchiveSystem7xDB.Linq.T_BInfoDal.Get_SPInfo(-1, -1, -1, null, null, null, null, null, -1, out count, 0, SGArchiveSystem7x.App_Code.CWebCommons._iPageRows, user.U_ID);
            var json = new
            {
                rows = list.ToArray(),
                total = count
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public string DownLoadxls()
        {
            var listWhere = SGArchiveSystem7x.App_Code.CSession.Get_QueryWhere(Session);
            var user = App_Code.CSession.Get_User(Session);
            string SaveUrl = "~/Content/xls/" + DateTime.Now.Ticks.ToString() + ".xlsx";
            int count = 0;
            var list = SGArchiveSystem7xDB.Linq.T_BInfoDal.Get_SPInfo(-1, -1, int.Parse(listWhere[1]), null, null, listWhere[3], listWhere[4], listWhere[0], int.Parse(listWhere[2]), out count, -1, -1, user.U_ID);
            Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
            Aspose.Cells.Cells cells = workbook.Worksheets[0].Cells;
            cells[0, 0].PutValue("档号");
            cells[0, 1].PutValue("档案类型");
            cells[0, 2].PutValue("题名");
            cells[0, 3].PutValue("借阅人");
            cells[0, 4].PutValue("借阅时间");
            cells[0, 5].PutValue("借阅目的");
            for (int m = 1; m <= list.Count(); m++)
            {
                cells[m, 0].PutValue(list[m - 1].BI_DH);
                if (list[m - 1].BU_TYPE != -1)
                {
                    var s = SGArchiveSystem7x.Models.CComboboxModel.Create_ComboboxArchiveTree(false);
                    foreach (var item in s.Where(c => c.ID == list[m - 1].BU_TYPE))
                    {
                        cells[m, 1].PutValue(item.Text);
                    }
                }
                cells[m, 2].PutValue(list[m - 1].BI_TM);
                cells[m, 3].PutValue(list[m - 1].BU_BUSERNAME);
                cells[m, 4].PutValue(list[m - 1].BI_YJGHDATE.ToString("yyyy-MM-dd HH:mm:ss"));
                cells[m, 5].PutValue(list[m - 1].BI_ATYPE);
            }
            workbook.Save(Server.MapPath(SaveUrl));
            return SaveUrl.Replace("~", "..");
        }
        public string brrtablexls(int id)
        {
            var list= SGArchiveSystem7xDB.Linq.T_SPFlowDal.Get_Brr(id);
            Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
            string fileUrl = Server.MapPath("~/Content/Outxls/档案借阅审批表模板.xlsx");
            workbook.Open(fileUrl);
            Aspose.Cells.Cells cells = workbook.Worksheets[0].Cells;
            cells[1, 1].PutValue(list.SPF_UserDep);
            cells[1, 3].PutValue(list.BU_BUSERNAME);
            cells[2, 1].PutValue(list.BI_DH);
            cells[2, 3].PutValue(list.BI_TM);
            cells[3, 1].PutValue(list.BI_V01);
            cells[3, 3].PutValue(list.BU_LYMD);
            string filename = DateTime.Now.Ticks.ToString();
            workbook.Save(Server.MapPath("~/Content/xls/" + filename + ".xlsx"));
            return "../Content/xls/" + filename + ".xlsx";
        }


        /// <summary>
        /// 取消出库操作，即结束借阅
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string EndCK(int id)
        {
            var user = App_Code.CSession.Get_User(Session);
            return SGArchiveSystem7xDB.Linq.T_BInfoDal.EndCK(id, user.U_RNAME, user.U_ID).ToString();
        }
    }
}